package com.ymatou.iapi.liveseller.testcase;

import java.util.List;
import java.util.Random;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.ymatou.iapi.liveseller.parameter.CloseSellerLiveBean;
import com.ymatou.iapi.liveseller.service.CloseSellerLiveCall;
import com.ymatou.iapi.liveseller.service.LiveProductListCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.livevideo.parameter.LiveNotifyBean;
import com.ymatou.livevideo.parameter.StreamLiveNotifyBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*买手关闭直播/结束直播
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CloseSellerLive{
	private static CloseSellerLiveBean closesellerliveBean;
	private static CloseSellerLiveCall closesellerliveCall;
	private static LiveRoomWapper livesWapper ;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手关闭直播/结束直播");
	}

	@Before
	public void caseUp() {
		closesellerliveBean=new CloseSellerLiveBean();
		closesellerliveCall=new CloseSellerLiveCall();
		livesWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(closesellerliveCall.getOpurl());
		livesWapper=null;
	}

	@After
	public void caseDown() {
		
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseSellerLive_001() {
		Logger.start(true,"买手关闭直播成功,直播商品删除");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("CloseLive");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();			
			Logger.verifyEquals("200", closesellerliveCall.getString("Code"), "验证Code");
			createlive=livesWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(8, createlive.getLiveStatus() ,"验证LiveStatus");
			LiveProductListCall liveProductListCall = LiveRoomCallService.GetLiveProductList(createlive.getId(), createlive.getUserId(), 2);
			Logger.verifyEquals(0, liveProductListCall.getSuspendCount(), "验证关闭直播时候商品删除SuspendCount");
			Logger.verifyEquals(0, liveProductListCall.getSoldOutCount(), "验证关闭直播时候商品删除SoldOutCount");
			Logger.verifyEquals(0, liveProductListCall.getOnsaleCount(), "验证关闭直播时候商品删除OnsaleCount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_002() {
		Logger.start(true,"买手结束直播成功");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			
			LiveRoomWapper liveRoomWapper = new LiveRoomWapper();
			liveRoomWapper.updateLiveRoomTime(YMTDateUtil.getBeforeOrNextMunite(-100), YMTDateUtil.getBeforeOrNextMunite(-50), createlive.getId());
			
			
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("FinishLive");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();
			Logger.verifyEquals("200", closesellerliveCall.getString("Code"), "验证Code");
			createlive=livesWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(9, createlive.getLiveStatus() ,"验证LiveStatus");
			//1215需求不下架
			LiveProductListCall liveProductListCall = LiveRoomCallService.GetLiveProductList(createlive.getId(), createlive.getUserId(), 2);
			Logger.verifyEquals(0, liveProductListCall.getSuspendCount(), "验证关闭直播时候商品暂停销售SuspendCount");
			Logger.verifyEquals(0, liveProductListCall.getSoldOutCount(), "验证关闭直播时候商品暂停销售SoldOutCount");
			Logger.verifyEquals(1, liveProductListCall.getOnsaleCount(), "验证关闭直播时候商品暂停销售OnsaleCount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_003() {
		Logger.start(true,"不输入liveid失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
//			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("CloseLive");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();			
			Logger.verifyEquals("400", closesellerliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("直播Id参数错误", closesellerliveCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_004() {
		Logger.start(true,"不输入UserId失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			closesellerliveBean.setLiveId(createlive.getId());
//			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("CloseLive");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();			
			Logger.verifyEquals("400", closesellerliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("买手Id参数错误", closesellerliveCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_005() {
		Logger.start(true,"输入liveid和userid不匹配失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId()+1);
			closesellerliveBean.setCloseType("CloseLive");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();			
			Logger.verifyEquals("201", closesellerliveCall.getString("Code"), "验证Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_006() {
		Logger.start(true,"不输入CloseType失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
//			closesellerliveBean.setCloseType("CloseLive");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();			
			Logger.verifyEquals("400", closesellerliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("不能为空", closesellerliveCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_007() {
		Logger.start(true,"买手未开始结束直播提示直播未开始，无法结束");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("FinishLive");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();
			Logger.verifyEquals("201", closesellerliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("直播未开始，无法结束", closesellerliveCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_008() {
		Logger.start(true,"买手已经开始关闭直播失败,应该结束直播");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			//操作数据库更新开始时间 这里可能有时间差
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("CloseLive");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();
			Logger.verifyEquals("201", closesellerliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("", closesellerliveCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseSellerLive_009() {
		Logger.start(true,"重复关闭直播,数据不会异常");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("CloseLive");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();
			closesellerliveCall.callService();	
			Logger.verifyEquals("201", closesellerliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("已关闭", closesellerliveCall.getString("Msg"), "验证Msg");
			createlive=livesWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(8, createlive.getLiveStatus() ,"验证LiveStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_010() {
		Logger.start(true,"未到时间买手结束直播失败");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("FinishLive");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();
			Logger.verifyEquals("201", closesellerliveCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("未到", closesellerliveCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}